// run: $exec < input
// opt: 0
// flag: -g
#include <iostream>
#include <algorithm>
#include <vector>

std::vector<std::vector<int>> evil;

int n;

struct team { int def, tot; };
bool operator<(team const& a, team const& b)
{
	return a.def < b.def || (a.def == b.def && a.tot > b.tot);
}

int calc()
{
	std::vector<team> v;
	for (int i = 0; i < n; i++) {
		team tmp; tmp.def = 0; tmp.tot = evil[i].size();
		for (int j = 0; j < (int)evil[i].size(); j++)
			tmp.def = std::max(tmp.def, evil[i][j] - j);
		v.push_back(tmp);
	}
	std::sort(v.begin(), v.end());
//	for (auto i : v) std::cout << i.def << ' ' << i.tot << '\n';
	int ret = 0, now = 0;
	for (int i = 0; i < n; i++) {
		if (v[i].def > now) {
			ret += v[i].def - now;
			now += v[i].def - now;
		}
		now += v[i].tot;
	}
	return ret;
}

int main()
{
	std::ios::sync_with_stdio(false);
	std::cin >> n;
	evil.resize(n);
	for (int i = 0; i < n; i++) {
		int m;
		std::cin >> m;
		for (int j = 0, x; j < m; j++) {
			std::cin >> x;
			evil[i].push_back(x);
		}
	}

	int q; std::cin >> q;
	for (int i = 0; i < q; i++) {
		int x, a, y, b;
		std::cin >> x >> a >> y >> b;
		x--; y--;
		std::vector<int> ta;
		std::vector<int> tb;
		for (int j = 0; j < a; j++) tb.push_back(evil[x][j]);
		for (int j = 0; j < b; j++) ta.push_back(evil[y][j]);
		for (int j = a; j < (int)evil[x].size(); j++) ta.push_back(evil[x][j]);
		for (int j = b; j < (int)evil[y].size(); j++) tb.push_back(evil[y][j]);
		evil[x] = ta;
		evil[y] = tb;
		std::cout << calc() << '\n';
	}
}

